<?php

/**
 * Created by PhpStorm.
 * Script Name: EventAuthorizedHandler.php
 * Create: 2020/4/15 10:54
 * Description: 快速注册小程序处理器
 * Author: fudaoji<fdj@kuryun.cn>
 */
namespace plugin\wechat\app\platform\handler\platform;

use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
use plugin\wechat\app\service\Mini as MiniService;
use plugin\wechat\app\service\MiniReg as RegService;


class EventThirdFastRegisteredBetaAppHandler implements EventHandlerInterface
{
    /**
     * 处理器
     * @param mixed $payload
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author fudaoji<fdj@kuryun.cn>
     */
    public function handle($payload = null) {
        /**
         * 成功：
         <xml>
        <AppId><![CDATA[第三方平台appid]]></AppId>
        <CreateTime>1535442403</CreateTime>
        <InfoType><![CDATA[notify_third_fastregisterbetaapp]]></InfoType>
        <appid>创建小程序appid<appid>
        <status>0</status>
        <msg>OK</msg>
        <info>
        <unique_id><![CDATA[unique_id]]></unique_id>
        <name><![CDATA[小程序名称]]></name>
        </info>
        </xml>
        失败：
         * {"AppId":"wx7ec884d7ea3bb23b","CreateTime":"1702459396","InfoType":"notify_third_fasteregister","status":"1007","msg":"id card exceed register limits","info":{"name":"","code":"","code_type":"1","legal_persona_wechat":"","legal_persona_name":"","component_phone":""}}
         * 
         */
        
        dao_log()->error('注册试用小程序事件：'.json_encode($payload, JSON_UNESCAPED_UNICODE));
        var_dump('注册试用小程序事件：'.json_encode($payload, JSON_UNESCAPED_UNICODE));
        
        $update = [
            'status' => 0
        ];
        $info = $payload['info'];
        $data = RegService::getByUniqueId($info['unique_id']);

        if(empty($data)){
            return false;
        }
        if(isset($payload['status']) && $payload['status'] == 0){
            $update['status'] = 1;
            $update['appid'] = $payload['appid'];
            $update['nick_name'] = $info['name'];
        }else{
            $update['fail_reason'] = $payload['msg'];
        }

        $map['id'] = $data['id'];
        RegService::updateInfo($update, $map);
        return true;
    }
}